#ifndef _SENSOR_AFE_H_
#define _SENSOR_AFE_H_

#include "bsp_gpio.h"
#include "global_resources.h"

#define SPEED_10HZ_DIF_SIGNAL_GET               1
#define SPEED_40HZ_TEMP_GET                     2
#define SPEED_40HZ_DIF_SIGNAL_GET               3

#define Device_Offset                           -769730

typedef struct {
    float           KNOWN_TEMP;
    uint8_t         CALIBRATION_SAMPLES;
    int32_t         TEMP_OFFSET;
    float           TEMP_SENSITIVITY;
    uint8_t         MODE;
} AFE_Tempareture_Param_Typedef_t;

typedef struct {
    float           KNOWN_SIGNAL;
    uint8_t         CALIBRATION_SAMPLES;
    int32_t         OFFSET;
    float           SENSITIVITY;
    uint8_t         MODE;
} AFE_DIF_SIGNAL_Param_Typedef_t;

class SensorAFE {
private:
    BSP_GPIO_Typedef_t afe_sck_config = {
            .port = GPIOA,
            .GPIO_Pin = GPIO_Pin_1,
            .GPIO_Speed = GPIO_Speed_50MHz,
            .GPIO_Mode = GPIO_Mode_Out_PP,
            .RCC_APBxPeriph = RCC_APB2Periph_GPIOA,
            .RCC_APBxPeriphClockCmd = RCC_APB2PeriphClockCmd
    };

    BSP_GPIO_Typedef_t afe_dout_config = {
            .port = GPIOA,
            .GPIO_Pin = GPIO_Pin_0,
            .GPIO_Speed = GPIO_Speed_50MHz,
            .GPIO_Mode = GPIO_Mode_IPU,
            .RCC_APBxPeriph = RCC_APB2Periph_GPIOA,
            .RCC_APBxPeriphClockCmd = RCC_APB2PeriphClockCmd
    };

    BSP_GPIO    afe_sck;
    BSP_GPIO    afe_dout;

    void           Init();

    void           wait_data_ready();
    int16_t        temperature_data_to_signed(uint32_t raw_data);
    int32_t        dif_signal_to_signed(uint32_t raw_data);

    int32_t        calibrate_dif_signal_zero_offset(AFE_DIF_SIGNAL_Param_Typedef_t calib_param);

public:

        AFE_Tempareture_Param_Typedef_t afe_temp_param = {
                .KNOWN_TEMP = 25.0f,
                .CALIBRATION_SAMPLES = 10,
                .TEMP_OFFSET = 0,
                .TEMP_SENSITIVITY = 20.04,
                .MODE = SPEED_40HZ_TEMP_GET
        };

        AFE_DIF_SIGNAL_Param_Typedef_t afe_dif_signal_param = {
                .KNOWN_SIGNAL = 98900.0f,
                .CALIBRATION_SAMPLES = 10,
                .OFFSET = 0,
                .SENSITIVITY = 1.0f,
                .MODE = SPEED_10HZ_DIF_SIGNAL_GET
        };

    SensorAFE();
    ~SensorAFE();

    uint32_t    read_row_data(uint8_t next_mode);

    void        calibrate_temperature_sensor(AFE_Tempareture_Param_Typedef_t afe_temp_param);
    void        calibrate_dif_signal_sensor(AFE_DIF_SIGNAL_Param_Typedef_t afe_atm_param);

    float       read_tempareture();
    float       read_dif_signal_value();

};

#endif
